package com.eshop.userorder.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.eshop.bean.DetailBean;
import com.eshop.bean.GoodsBean;
import com.eshop.bean.OrdersBean;
import com.eshop.dao.Conn;

public class SelectDetailDao {

	private Connection conn = null;
	private PreparedStatement sta = null;
	private ResultSet r = null;
	
	public ArrayList<DetailBean> selectDetail(Integer ordersId)
	{
		
		 conn = Conn.getConn();
		 String sql = "SELECT orders.total_money,detail.goods_amount,goods.goods_name,goods.goods_img,goods.goods_price FROM goods INNER JOIN detail ON detail.goods_id = goods.goods_id INNER JOIN orders ON detail.orders_id = orders.orders_id WHERE orders.orders_id = ? ";
		 ArrayList<DetailBean> detailBeanList = new ArrayList<DetailBean>();
		 try {

			 sta =  conn.prepareStatement(sql);
			 sta.setObject(1, ordersId);
			 r = sta.executeQuery();
			 while(r.next())
			 {
				 String goodsName = r.getString("goods_name");
				 String goodsImg = r.getString("goods_img");
				 Float goodsPrice = r.getFloat("goods_price");
				 GoodsBean goodsBean = new GoodsBean();
				 goodsBean.setGoodsName(goodsName);
				 goodsBean.setGoodsImg(goodsImg);
				 goodsBean.setGoodsPrice(goodsPrice);
				 
				 
				 Float totalMoney = r.getFloat("total_money");
				 OrdersBean ordersBean = new OrdersBean();
				 ordersBean.setTotalMoney(totalMoney);
				 
				 
				 Integer goodsAmount = r.getInt("goods_amount");
				 
				 DetailBean detailBean = new DetailBean();
				 
				 detailBean.setGoodsAmount(goodsAmount);
				 detailBean.setOrders(ordersBean);
				 detailBean.setGoods(goodsBean);
				 
				 detailBeanList.add(detailBean);
			 } 
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally{
			try {
				if(r != null)
				{
					r.close();
				}
				if(sta != null)
				{
					sta.close();
				}
				if(conn != null)
				{
					conn.close();
				}
	} catch (SQLException e) {
		e.printStackTrace();
		
	}
}		
		return detailBeanList;
	}
}
